layui.define(function (exports) {
  "use static";

  var config = {
    init: function () {
      config.tableInit(); //table全局初始化
    },
    tableInit: function () {
      var table = layui.table,
        obj = config.getToken();
      if (obj && obj.token) {
        table.set({
          headers: { Authorization: "Bearer " + obj.token },
        });
      } else {
        table.set({
          request: {
            pageName: "pageNo", //页码的参数名称，默认：page
            limitName: "pageSize", //每页数据量的参数名，默认：limit
          },
          limit: 20,
          size: "sm",
          //, skin: 'line'
          //, even: true
          parseData: function (res) {
            //res 即为原始返回的数据
            return {
              code: 0, //解析接口状态
              msg: res.msg, //解析提示文本
              // "count": res.attach.total, //解析数据长度
              count: res.count, //解析数据长度
              data: res.data, //解析数据列表
            };
          },
        });
      }
    },
    cacheName: "plus-admin",
    baseServer: "/plus-admin",
    localServer: "http://127.0.0.1:5500",
    imgServer: "/",
    pageTabs: true, // 多页签
    topMenu: true, //顶部通栏布局
    showTabs: false, //显示页签栏
    maxTabNum: 20, // 页签数
    cacheTab: false, // 记忆页签 开启多页签有效
    autoRefresh: false, //自动刷新
    theme: "sapphire", // 主题：宝石蓝
    putToPost: false, // 请求put方法变成post
    apiNoCache: false, // api缓存
    copyright: false, //版权
    loader: 1, //默认加载
    getToken: function () {
      var cache = layui.data(this.cacheName);
      if (cache) return cache.token;
    },
    putToken: function (token) {
      layui.data(this.cacheName, { key: "token", value: token });
    },
    removeToken: function () {
      layui.data(this.cacheName, { key: "token", remove: true });
    },
    getUser: function () {
      var cache = layui.data(this.cacheName);
      if (cache) return cache.loginUser;
    },
    putUser: function (user) {
      layui.data(this.cacheName, { key: "loginUser", value: user });
    },
    getUserAuths: function () {
      var auths = [],
        user = this.getUser();
      var authorities = user ? user.authorities : [];
      for (var i = 0; i < authorities.length; i++) {
        auths.push(authorities[i].authority);
      }
      return auths;
    },
    routerNotFound: function (r) {
      let html =
        '不存在路由【 <span class="text-danger">' +
        r.path.join("/") +
        "</span> 】，请确认！";
      layer.alert(html, {
        title: "温馨提示",
        offset: "45px",
        shadeClose: true,
      });
    },
    getRequestHeaders: function (url) {
      var headers = [];
      var token = this.getToken();
      if (token)
        headers.push({ name: "Authorization", value: "Bearer " + token });
      return headers;
    },
    getAjaxBefore: function (res, url, obj) {
      if (res.code === 401) {
        config.removeToken();
        layer.msg("登录过期了", { icon: 2, anim: 6, time: 1200 }, function () {
          location.replace("../../login.html");
        });
        return false;
      }
      return true;
    }
  };

  config.init(); //初始化
  
  exports("plusConfig", config);
});
